import router from '@system.router';

var timerDuration = null;
var timerRhythm = null;
var timerPercent = null;
var rhythm = 0;
var times = 0;

export default {
    data: {
        period: "",
        cycles: 0,
        breath: "慢跑",
        percent: 0,
        visible: true,
        duration: 0
    },
    onInit() {
        console.log("训练页面正在初始化...");

        console.log("时长参数：" + this.durationSelected);
        console.log("节奏参数：" + this.rhythmSelected);

        if (this.durationSelected == 0)
            this.duration = /*60*/10;
        else if (this.durationSelected == 1)
            this.duration = 120;
        else if (this.durationSelected == 2)
            this.duration = 180;

        if (this.rhythmSelected == 0)
            rhythm = 6;
        else if (this.rhythmSelected == 1)
            rhythm = 4;
        else if (this.rhythmSelected == 2)
            rhythm = 2;

        times = this.duration / rhythm;

        this.period = rhythm + "s";
        this.cycles = times;
    },
    onReady() {
        console.log("训练页面就绪...");
    },
    onShow() {
        console.log("训练页面正在显示...");

        timerDuration = setInterval(this.onDurationTimer, 1000);
        timerRhythm = setInterval(this.onRhythmTimer, rhythm * 1000);
        timerPercent = setInterval(this.onPercentTimer, rhythm / 100 * 1000);
    },
    onDestroy() {
        console.log("训练页面正在销毁...");

        clearInterval(timerDuration);
        timerDuration = null;
        clearInterval(timerRhythm);
        timerRhythm = null;
        clearInterval(timerPercent);
        timerPercent = null;
    },
    onSwipe(e) {
        if (e.direction == "right")
            router.replace({uri: "pages/emotion/emotion"});
    },
    onClick() {
        router.replace({uri: "pages/index/index"});
    },
    onDurationTimer() {
        this.duration--;

        if (this.duration == 0) {
            clearInterval(timerDuration);
            timerDuration = null;

            this.visible = false;
        }
    },
    onRhythmTimer() {
        times--;

        if (times == 0) {
            clearInterval(timerRhythm);
            timerRhythm = null;
            clearInterval(timerPercent);
            timerPercent = null;

            this.breath = "已完成";
            this.percent = 100;
        }
        else if (this.breath == "慢跑") {
            this.breath = "快跑";
            this.percent = 0;
        }
        else if (this.breath == "快跑") {
            this.breath = "慢跑";
            this.percent = 0;
        }
    },
    onPercentTimer() {
        this.percent++;
    }
}
